package com.elm.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.elm.model.User;
/**
 * 
 * @author pgm
 *
 */
import com.elm.service.UserService;
import com.elm.serviceimpl.UserServiceImpl;
public class UpdateUserInfoController extends HttpServlet{
	UserService userservice = new UserServiceImpl();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		req.setCharacterEncoding("utf8");
		resp.setCharacterEncoding("utf8");
		HttpSession session = req.getSession();
		User user = (User) session.getAttribute("currentUser");
		String  oldpassword = user.getUserpsw();
		//ajax err
		PrintWriter pw = resp.getWriter();
		//判断旧密码是否输入正确
		if(oldpassword.equals(req.getParameter("oldpassword"))){
			if(req.getParameter("newpassword1").equals(req.getParameter("newpassword2"))){
				String newpsw = req.getParameter("newpassword1");
				String newaddr = req.getParameter("newaddress");
				String newphone = req.getParameter("newphone");
				userservice.updateUserInfobyUserid(newpsw, newaddr, newphone, user.getUserid());
				pw.write("1");
			}else{
				req.getRequestDispatcher("user/updateuserinfo.jsp").forward(req, resp);
			}
			
		}else{
			req.getRequestDispatcher("user/updateuserinfo.jsp").forward(req, resp);
		}
		
	}
}
